/*
 * Package externalModules
 * File SipEventPackage.java
 * Created on Jul 28, 2005
 *
 */
package il.ac.biu.cs.grossmm.api.simple;

import il.ac.biu.cs.grossmm.api.flow.DataProcessorBase;
import il.ac.biu.cs.grossmm.api.presence.ResourceType;
import il.ac.biu.cs.grossmm.api.server.ComponentManager;
import il.ac.biu.cs.grossmm.api.server.ServiceManager;
import il.ac.biu.cs.grossmm.api.simple.RequestProcessException;

import javax.sip.SipFactory;
import javax.sip.header.HeaderFactory;
import javax.sip.message.Request;
import javax.sip.message.Response;

import org.apache.log4j.Logger;

/**
 * An abstract base for <tt>SipEventPackage</tt> implementations.
 * Superclasses must implement the following methods:
 * <ol>
 * <li>getMimeTypes
 * <li>getPackageName
 * <li>getResourceType
 * <li>isTemplatePackage
 * <li>subscribe
 * </ol>
 */
public abstract class SipEventPackageBase extends DataProcessorBase implements
		SipEventPackage {
	static Logger logger = Logger.getLogger(SipEventPackageBase.class);

	protected SipNotificationProvider enProvider = null;

	protected SipSharedProvider provider = null;

	protected HeaderFactory headerFactory;

	SipSubscription pendingSubscription;

	/*
	 * ... This SUBSCRIBE request will be confirmed with a final response.
	 * 200-class responses indicate that the subscription has been accepted, and
	 * that a NOTIFY will be sent immediately. A 200 response indicates that the
	 * subscription has been accepted and that the user is authorized to
	 * subscribe to the requested resource. A 202 response merely indicates that
	 * the subscription has been understood, and that authorization may or may
	 * not have been granted.
	 * 
	 * ...
	 * 
	 * Non-200 class final responses indicate that no subscription or dialog has
	 * been created, and no subsequent NOTIFY message will be sent. All non-200
	 * class responses (with the exception of "489", described herein) have the
	 * same meanings and handling as described in SIP (RFC 3261) ...
	 */

	/**
	 * The CTOR
	 * 
	 * @param moduleName
	 *            Event package human readable module name
	 * @param versionMajor
	 *            major module name
	 * @param versionMinor
	 *            minod module name
	 */
	protected SipEventPackageBase(String moduleName) {
		super(moduleName, false);
	}

	@Override
	public void setComponentManager(ComponentManager container)
			throws Exception {
		super.setComponentManager(container);

		enProvider = componentManager.getByClass(SipNotificationProvider.class);
		provider = enProvider.getSharedProvider();
		headerFactory = SipFactory.getInstance().createHeaderFactory();
	}

	@Override
	public synchronized void start(ServiceManager serviceManager)
			throws Exception {
		enProvider.addPackage(this);
		super.start(serviceManager);
	}

	@Override
	public synchronized void stop(Exception exception) {
		super.stop(exception);
		enProvider.removePackage(this);
	}

	protected ResourceType getNestedResourceType(String eventType)
			throws RequestProcessException {
		assert (eventType.endsWith(getPackageName()));

		String nestedEventType = eventType.substring(0, eventType.length()
				- getPackageName().length() - 1);

		return enProvider.getResourceType(nestedEventType);
	}

	/**
	 * Should be normally overriden
	 */
	public int getDefaultExpiration() {
		return 3600;
	}

	public int getMinimumExpiration() {
		return 0;
	}

	public Response refresh(Request subscribeReq, SipSubscription sipSub) {
		return null;
	}

}
